//////////////////////////////////////////
//
//	FlexInc. 2011
//	http://www.loziosecchi.it/flex_inc
//	http://twitter.com/loziosecchi
//
//////////////////////////////////////////

package flexInc.spark.effects
{
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import mx.effects.IEffectInstance;
	
	import spark.effects.AnimateTransitionShader;
	
	/**
	 *  The Wipe effect uses Pixel Bender, which is not supported
	 *  for AIR mobile applications.
	 */
	[DiscouragedForProfile("mobileDevice")]

	/**
	 *  The CircleWipe effect performs a bitmap transition effect by running a
	 *  directional <i>circular wipe</i> between the first and second bitmaps.
	 *  This wipe exposes the second bitmap over the course of the 
	 *  animation in a direction indicated by the <code>direction</code>
	 *  property.
	 * 
	 *  <p>A pixel-shader program loaded by the effect
	 *  runs the underlying bitmap effect. 
	 *  If you want to use 
	 *  a different CircleWipe behavior, you can specify a custom pixel-shader program. 
	 *  The pixel-shader program must adhere to the constraints 
	 *  specified for the <code>shaderByteCode</code>
	 *  property of AnimateTransitionShader class, and supply three additional
	 *  parameters. 
	 *  The extra parameters required by the CircleWipe shader 
	 *  are:</p>
	 *
	 *  <ul>
	 *    <li>An int <code>direction</code> parameter, 
	 *  whose value means the same as the related String property
	 *  in the CircleWipe class.</li>
	 *    <li>Two floating point parameters: 
	 *  <code>imageWidth</code> and <code>imageHeight</code>. </li>
	 *  </ul>
	 *
	 *  <p>All of these parameters are set on the shader when the effect starts playing,
	 *  so the parameters need to exist and do something appropriate in
	 *  order for the effect to function correctly.</p>
	 *  
	 *  @see flexInc.spark.effects.CircleWipeDirection
	 *  @see spark.effects.AnimateTransitionShader
	 *  @see spark.effects.AnimateTransitionShader#shaderByteCode
	 *  
	 *  @langversion 3.0
	 *  @playerversion Flash 10
	 *  @playerversion AIR 1.5
	 *  @productversion Flex 4
	 */
	public class CircleWipe extends AnimateTransitionShader
	{
		[Embed(source="CircleWipe.pbj", mimeType="application/octet-stream")]
		private static var CircleWipeShaderClass:Class;
		private static var circleWipeShaderCode:ByteArray = new CircleWipeShaderClass();
		[Embed(source="CircleWipeReverse.pbj", mimeType="application/octet-stream")]
		private static var CircleWipeReverseShaderClass:Class;
		private static var circleWipeReverseShaderCode:ByteArray = new CircleWipeReverseShaderClass();

		[Inspectable(enumeration="normal,reverse", defaultValue="normal")]
		/**
		 *  The direction that the wipe moves during the animation: 
		 *  <code>CircleWipeDirection.NORMAL</code>, <code>CircleWipeDirection.REVERSE</code>, 
		 *
		 *  @default CircleWipeDirection.NORMAL
		 *
		 *  @see CircleWipeDirection#NORMAL
		 *  @see CircleWipeDirection#REVERSE
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public var direction:String = CircleWipeDirection.NORMAL;

		/**
		 *  The center of the effect; if the <i>x</i> or <i>y</i> values are greater
		 *  than the bitmap width, they will be positioned inside the <i>width</i> and
		 *  <i>height</i> values accordingly.
		 *
		 *  @default Point(0, 0)
		 *  
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public var center:Point = new Point(0, 0);
		
		/**
		 *  Constructor. 
		 *
		 *  @param target The Object to animate with this effect.  
		 *
		 *  @langversion 3.0
		 *  @playerversion Flash 10
		 *  @playerversion AIR 1.5
		 *  @productversion Flex 4
		 */
		public function CircleWipe(target:Object=null)
		{
			super(target);
		}

		/**
		 *  @private
		 */
		override protected function initInstance(instance:IEffectInstance):void
		{
			switch (direction)
			{
				case CircleWipeDirection.NORMAL:
					shaderByteCode = circleWipeShaderCode;
					break;
				case CircleWipeDirection.REVERSE:
					shaderByteCode = circleWipeReverseShaderCode;
					break;
			}
			
			var properties:Object = new Object();
			properties.centerX = center.x;			
			properties.centerY = center.y;			
			shaderProperties = properties;

			super.initInstance(instance);
		}
	}
}